package com.zelin.controller;

import com.alibaba.fastjson.JSON;
import com.zelin.pojo.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.time.ZonedDateTime;
import java.util.List;

/**
 * Created by WF on 2020/10/29 11:34
 */
@RestController
@Slf4j
@RequestMapping("order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    public static final String URL = "http://service-product";
    //1.根据id查询商品
    @GetMapping("/product/{pid}")
    public Product findProductById(@PathVariable Integer pid){
        //1.0)得到当前服务的实例
        //1.1)根据id查询商品
        String url = URL + "/product/" + pid;
        System.out.println("url = " + url);
        Product product = restTemplate.getForObject( url, Product.class);

        //1.2)打印商品
        //log.info(">>商品信息,查询结果:{}-->端口{}" + JSON.toJSONString(product),port);

        //1.3)返回商品
        return product;
    }

}
